﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections;
public class lang
{
	public lang()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static string getLang()
    {
        string lang="vn";
        lang = (System.Web.HttpContext.Current.Request["lang"] == null) ? "vn" : System.Web.HttpContext.Current.Request["lang"].ToString().Trim();
        return lang;
    }
    

    public static string getVariableValue(string VariableName)
    {
        return getVariableValue("default", VariableName);
    }

    public static string getVariableValue(string fn, string VariableName)
    {
        try
        {
            //Định vị ngôn ngữ
            string lang = "vn";
            lang = getLang();
            DataSet ds = new DataSet();

            //if (System.Web.HttpContext.Current.Cache["Lang_" + lang] == null)
            //{
                //Xác định đường dãn
                string sPath = "";
                sPath = comm.getPathRoot() + "\\files\\language\\" + lang + "\\" + fn + ".xml";

                ds.ReadXml(sPath);
               // System.Web.HttpContext.Current.Cache.Insert("Lang_" + lang, ds, null, DateTime.Now.AddMinutes(comm.GetVariantCacheTimeOutLevel2()), System.Web.Caching.Cache.NoSlidingExpiration);
            //}
            //else
            //{
            //    ds = (DataSet)System.Web.HttpContext.Current.Cache["Lang_" + lang];
            //}

            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (VariableName.ToLower() == dt.Rows[i]["name"].ToString().Trim().ToLower())
                {
                    if (dt.Rows[i]["value"].ToString().Trim() != "")
                    {
                        return dt.Rows[i]["value"].ToString();
                    }
                    else
                    {
                        return "Chưa có nhãn";
                    }
                }
            }
            return "Chưa có nhãn";
        }
        catch
        {
            return "Lỗi đọc file ngôn ngữ";
        }
    }


    // 
    public static string GetCateNameTitle(short idVi, short idEN)
    {
        try
        {
            //Định vị ngôn ngữ
            string lang = "vn";
            lang = getLang();
            string strSQL = "";
            if (lang == "vn")
            {
                strSQL = "SELECT  * From Cats where ID=" + idVi + "  and lang = " + comm.to_sql(lang, "text") + " ";
            }
            else
            {
                strSQL = "SELECT  * From Cats where ID=" + idEN + "  and lang = " + comm.to_sql(lang, "text") + " ";
            }
            DataTable dt = dal.SelectTable(strSQL);
            //if (lang == "vn")
            //{
                return dt.Rows[0]["CatName"].ToString();
            //}
            //else
            //{
            //    return  dt.Rows[0]["CatNameEng"].ToString();
            //}
        }
        catch (Exception ex)
        {
            return "";
        }
    }
    public static string GetInforWeb(string nameField)
    {
        try
        {
            //Định vị ngôn ngữ
            string lang = "vn";
            lang = getLang();
            string strSQL = "";
            strSQL = "SELECT  * From Webvariants"; //where ID=" + id + "  and lang = " + comm.to_sql(lang, "text") + " ";
            DataTable dt = dal.SelectTable(strSQL);
            //if (lang == "vn")
            //{
            return dt.Rows[0][nameField].ToString();
            //}
            //else
            //{
            //    return  dt.Rows[0]["CatNameEng"].ToString();
            //}
        }
        catch (Exception ex)
        {
            return "";
        }
    }
}
